﻿using System;
using System.Data;
using System.IO;
using System.Linq;
using System.Xml;
using System.Xml.Linq;

namespace SSISComponents
{
    class GoogleDimensions
    {
        private DataTable _Dimensions;

        public DataTable Dimensions
        {
            get
            {
                return _Dimensions;
            }
        }

        public GoogleDimensions()
        {
            CreateDataTable();
            LoadXML();
        }

        private void CreateDataTable()
        {
            _Dimensions = new DataTable("Dimensions");

            DataColumn dcID = new DataColumn("ID", System.Type.GetType("System.String"));
            _Dimensions.Columns.Add(dcID);
            DataColumn dcName = new DataColumn("Name", System.Type.GetType("System.String"));
            _Dimensions.Columns.Add(dcName);
            DataColumn dcAPIName = new DataColumn("APIName", System.Type.GetType("System.String"));
            _Dimensions.Columns.Add(dcAPIName);
         
        }

        /// <summary>
        /// Load the dimensions XML ressource
        /// </summary>
        public void LoadXML()
        {
            MemoryStream ms = new MemoryStream(global::SSISComponents.Properties.Resources.Dimensions);
            var Dim = from d in XElement.Load(XmlTextReader.Create(ms)).Elements("Dimensions")
                      select d;
            foreach (var dimensions in Dim)
            {
                _Dimensions.Rows.Add(Row(dimensions.Element("ID").Value.ToString(), dimensions.Element("Name").Value.ToString(), dimensions.Element("APIName").Value.ToString()));
            }
            ms.Dispose();
        }

        public Boolean Exist(string APIName)
        {
            MemoryStream ms = new MemoryStream(global::SSISComponents.Properties.Resources.Dimensions);
            bool _APIName = false;
            try
            {
                var Dim = from d in XElement.Load(XmlTextReader.Create(ms)).Elements("Dimensions")
                          select d;
                foreach (var dAPIName in Dim)
                {
                    if (dAPIName.Element("APIName").Value.ToString() == APIName)
                    {
                        _APIName = true;
                    }
                }
            }
            catch { }
            return _APIName;
        }

        public string GetDescription(string APIName)
        {
            MemoryStream ms = new MemoryStream(global::SSISComponents.Properties.Resources.Dimensions);
            string _Description = null;
            try
            {
                var Dim = from d in XElement.Load(XmlTextReader.Create(ms)).Elements("Dimensions")
                          where d.Element("APIName").Value == APIName
                          select d;
                foreach (var dimensions in Dim)
                {
                    _Description = dimensions.Element("Description").Value.ToString();
                }
            }
            catch { }
            return _Description;
        }

        public string GetDataType(string APIName)
        {
            MemoryStream ms = new MemoryStream(global::SSISComponents.Properties.Resources.Dimensions);
            string _DataType = null;
            try
            {
                var Dim = from d in XElement.Load(XmlTextReader.Create(ms)).Elements("Dimensions")
                          where d.Element("APIName").Value == APIName
                          select d;
                foreach (var dimensions in Dim)
                {
                    _DataType = dimensions.Element("DataType").Value.ToString();
                }
            }
            catch { }
            return _DataType;
        }

        public string GetLength(string APIName)
        {
            MemoryStream ms = new MemoryStream(global::SSISComponents.Properties.Resources.Dimensions);
            string _Length = null;
            try
            {
                var Dim = from d in XElement.Load(XmlTextReader.Create(ms)).Elements("Dimensions")
                          where d.Element("APIName").Value == APIName
                          select d;

                foreach (var dimensions in Dim)
                {
                    _Length = dimensions.Element("Length").Value.ToString();
                }
            }
            catch { }
            return _Length;
        }

        public string GetPrecision(string APIName)
        {
            MemoryStream ms = new MemoryStream(global::SSISComponents.Properties.Resources.Dimensions);
            string _Precision = null;
            try
            {
                var Dim = from d in XElement.Load(XmlTextReader.Create(ms)).Elements("Dimensions")
                          where d.Element("APIName").Value == APIName
                          select d;
                foreach (var dimensions in Dim)
                {
                    _Precision = dimensions.Element("Precision").Value.ToString();
                }
            }
            catch { }
            return _Precision;
        }

        public string GetScale(string APIName)
        {
            MemoryStream ms = new MemoryStream(global::SSISComponents.Properties.Resources.Dimensions);
            string _Scale = null;
            try
            {
                var Dim = from d in XElement.Load(XmlTextReader.Create(ms)).Elements("Dimensions")
                          where d.Element("APIName").Value == APIName
                          select d;

                foreach (var dimensions in Dim)
                {
                    _Scale = dimensions.Element("Scale").Value.ToString();
                }
            }
            catch {}
            return _Scale;
        }

        public bool UnvalidMetrics(string DimensionsAPIName, string MetricsAPIName)
        {
            bool retValue = false;
            try
            {
                if (!String.IsNullOrEmpty(DimensionsAPIName) && !String.IsNullOrEmpty(MetricsAPIName))
                {
                    GoogleMetrics gaMetrics = new GoogleMetrics();
                    string DimensionsID = GetID(DimensionsAPIName);
                    string MetricsID = gaMetrics.GetID(MetricsAPIName);

                    MemoryStream msUnvalidCombination = new MemoryStream(global::SSISComponents.Properties.Resources.UnvalidCombinations);
                    var UC = from d in XElement.Load(XmlTextReader.Create(msUnvalidCombination)).Elements("UnvalidCombinations")
                             where d.Element("MetricsID").Value == MetricsID && d.Element("DimensionsID").Value == DimensionsID
                             select d.Elements("UCID");

                    var UCa = UC.ToArray();
                    for (int d = 0; d < UCa.Length; d++)
                    {
                        if (!String.IsNullOrEmpty(UCa[d].ToString()))
                        {
                            retValue = true;
                        }
                    }
                }
            }

            catch {}
            return retValue;
        }

        public string GetID(string APIName)
        {
            MemoryStream msDimensions = new MemoryStream(global::SSISComponents.Properties.Resources.Dimensions);
            var Dim = from d in XElement.Load(XmlTextReader.Create(msDimensions)).Elements("Dimensions")
                      where d.Element("APIName").Value == APIName
                      select d;
            string ID = null;
            foreach (var dimensions in Dim)
            {
                ID = dimensions.Element("ID").Value.ToString();
            }
            msDimensions.Dispose();
            return ID;
        }

        private DataRow Row(String ID, String Name, String APIName)
        {
            DataRow dr = _Dimensions.NewRow();
            dr["ID"] = ID;
            dr["Name"] = Name;
            dr["APIName"] = APIName;
            return dr;
        }

        public Boolean Check()
        {
            return true;
        }
    }
}